package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.GoodsMapper;
import com.example.finalsystem.Dao.ListGoodsMapper;

import com.example.finalsystem.Dao.OutListMapper;
import com.example.finalsystem.Entity.Goods;
import com.example.finalsystem.Entity.ListGoods;

import com.example.finalsystem.Entity.OutList;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import com.example.finalsystem.Service.GoodsService;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

@Service
public class OutListService {

    @Autowired
    GoodsService goodsService;

    private SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = OutListService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }
    public boolean hasFK_listGoodsId(Long listGoodsId,SqlSession sqlSession)
    {
//        SqlSession sqlSession = openSession();
        return  sqlSession.getMapper(OutListMapper.class).getAllByListGoodsId(listGoodsId)==null;
    }
    public String addOutList(Long buyListId,Integer exportNumber,Long FK_listGoodsId,SqlSession sqlSession )
    {
        OutList outList = new OutList();
        Random r = new Random();
        int number = r.nextInt(10);
        String str =""+(System.currentTimeMillis()/1000+number);
        outList.setOutlistid(str.substring(str.length()-6));
        outList.setExportnumber(exportNumber);
        outList.setState("未分发");
        outList.setFkListgoodsid(FK_listGoodsId);
        outList.setFkBuylistid(buyListId);

        sqlSession.getMapper(OutListMapper.class).insert(outList);
        sqlSession.commit();
//        sqlSession.close();
        return "添加成功";
    }


    public String fixOutList(String outListId,Integer exportNumber,
                             String logisticsInformation,String state){
        SqlSession sqlSession = openSession();
        OutList outList  = sqlSession.getMapper(OutListMapper.class).selectByPrimaryKey(outListId);
        if(outList == null) {
            return "没有找到该出货单";
        }
        if (outList.getState().equals("已分发"))
        {
            return "该出货单已出货无法修改";
        }
        outList.setExportnumber(exportNumber);
        outList.setState(state);
        outList.setLogisticsinformation(logisticsInformation);

        sqlSession.getMapper(OutListMapper.class).updateByPrimaryKey(outList);
//        仓库管理员  确认出货单 后
        if (state.equals("已分发"))
        {
            Long listGoodsId =  outList.getFkListgoodsid();
            ListGoods listGoods = sqlSession.getMapper(ListGoodsMapper.class).selectByPrimaryKey(listGoodsId);
            String goodsName = listGoods.getGoodsname();
            Goods goods = sqlSession.getMapper(GoodsMapper.class).selectByPrimaryKey(goodsName);
            goods.setGoodsnumber(goods.getGoodsnumber()-exportNumber);
            sqlSession.getMapper(GoodsMapper.class).updateByPrimaryKey(goods);
        }
        sqlSession.commit();
        sqlSession.close();
        return "修改出货单成功";
    }


    public String deleteOutList(String outListId)
    {
        SqlSession sqlSession = openSession();
        if (sqlSession.getMapper(OutListMapper.class).selectByPrimaryKey(outListId).getState().equals("已分发")){
            return "删除失败，出货单已经出货";
    }

        sqlSession.getMapper(OutListMapper.class).deleteByPrimaryKey(outListId);
        sqlSession.commit();
        sqlSession.close();
        return "删除成功";
    }


    public OutList getOutList(String outListId){
        SqlSession sqlSession = openSession();
        OutList outList = sqlSession.getMapper(OutListMapper.class).selectByPrimaryKey(outListId);
        sqlSession.close();
        return outList;
    }


    public List<OutList> getAllOutListByBuyListId(Long buyListId){
        SqlSession sqlSession = openSession();
        List<OutList> outList= sqlSession.getMapper(OutListMapper.class).getAllByBuyListId(buyListId);
        for (OutList o:outList
             ) {
            ListGoods listGoods =sqlSession.getMapper(ListGoodsMapper.class).selectByPrimaryKey(o.getFkListgoodsid());
            o.setTempName(listGoods.getGoodsname());
        }
        sqlSession.close();
        return outList;
    }

    public Integer countBuyNumber(Long listGoodId)
    {
        SqlSession sqlSession = openSession();
        List<OutList> outLists = sqlSession.getMapper(OutListMapper.class).getAllByListGoodsId(listGoodId);
        int count=0;
        for (OutList o: outLists
             ) {
            count+=o.getExportnumber();
        }
        return count;
    }


    public List<OutList> getAllOutListByListGoodsId(Long goodListId){
        SqlSession sqlSession = openSession();
        List<OutList> outLists = sqlSession.getMapper(OutListMapper.class).getAllByListGoodsId(goodListId);
        sqlSession.close();
        return outLists;
    }

    public List<OutList> getAllOutList()
    {
        SqlSession sqlSession = openSession();
        List<OutList> outLists = sqlSession.getMapper(OutListMapper.class).getAll();
        for (OutList o:outLists
        ) {
//            System.out.println(o.getOutlistid());
//            System.out.println(o.getFkListgoodsid());
            ListGoods listGoods =sqlSession.getMapper(ListGoodsMapper.class).selectByPrimaryKey(o.getFkListgoodsid());
//            System.out.println(listGoods);
//            System.out.println(listGoods.getGoodsname());
            o.setTempName(listGoods.getGoodsname());
            o.setCanOut(goodsService.canOut(o.getTempName(),o.getExportnumber()));
        }
        sqlSession.close();
        return  outLists;
    }
}
